/*
 * enc_shrm.h
 *
 *  Created on: 2018年10月16日
 *  Author: Degang.Wu (windragon@126.com)
 *  Desc:
 */

#ifndef ENC_SHRM_H_
#define ENC_SHRM_H_

#include <semaphore.h>
#include "enc_type.h"
#include <time.h>


#define ENC_MAX_CHL				(4)		//encoder channel per sub board
#define ENC_MAX_SUB_BOARD		(6)		//max number of sub board
#define ENC_TOTAL_CHLMAX		(ENC_MAX_CHL*ENC_MAX_SUB_BOARD)

#define ENC_MAX_IPINPUT_NUM		(32)

#define ENC_OSD_MAX_OVERLAY		(3)		//FIXME:is ok?
#define ENC_OSD_SUB_LEN			(256)
#define ENC_OSD_FONTNAME_LEN	(32)
#define ENC_OSD_MAX_SLIDE_NUM	(3)
#define ENC_OSD_MAX_TM_NUM		(3)		//period time set
#define ENC_WEEK_DAYS			(7)

#define ENC_SERVICE_NAME_LEN	(32)
#define ENC_PROVIDER_NAME_LEN	(32)

#define ENC_ETHIFMAX_MASTER		(5)
#define ENC_ETHIFMAX_SLAVE		(1)
#define ENC_ETHNAME_LEN			(32)
#define ENC_ETHIPMAX			(1)

#define ENC_SHRMEMKEY_M   	(201810161)
#define ENC_SHRMEMKEY_S   	(201810160)
#define ENC_SHMINITSEM  	"enc_shminitsem"
#define ENC_GDEVSEM     	"enc_gdevsem"
#define ENC_GLOGSEM    	 	"enc_glogsem"   //log update

#define ENC_MASTER_PORT		(15000)		// TCP sock listen port (master)
#define ENC_SLAVE_PORT		(15001)

//#define ENC_DFLT_MST_IP		"10.10.10.10"
#define ENC_DFLT_MST_IP		"192.168.2.30"	//for test

//#define ENC_DFLT_SLV0_IP	"10.10.10.100"
#define ENC_DFLT_SLV0_IP	"192.168.2.80"

#define ENC_CHL_OUT_IP		"192.168.2.200"
#define ENC_CHL_OUT_PORT	10001

enum
{
	ENC_ETHID_MANAGER 	= 0,
	ENC_ETHID_PRIV 		= 1,
	ENC_ETHID_DATA_IN	= 2,
	ENC_ETHID_DATA_OUT	= 3,
	ENC_ETHID_FPGA		= 4,
};

typedef enum ENC_STACODE_
{
	STACODE_OK,
	STACODE_ERROR,
	STACODE_ALARM,
	STACODE_CFGERR,
	STACODE_INPERR,
	STACODE_OUTERR
} eENC_STACODE;

typedef enum
{
	BWENC_VID_CODEC_INVALID = 0,

	BWENC_VID_CODEC_H264,
	BWENC_VID_CODEC_H265,

	BWENC_VID_CODEC_NB
} eVID_CODEC;

typedef enum
{
	BWENC_VID_PROFILE_INVALID = 0,

	BWENC_VID_PROFILE_BASELINE,
	BWENC_VID_PROFILE_MAIN,
	BWENC_VID_PROFILE_HIGH,

	BWENC_VID_PROFILE_NB
} eVID_PROFILE;

typedef enum
{
	BWENC_VID_RC_INVALID = 0,

	BWENC_VID_RC_ABR,
	BWENC_VID_RC_CBR,
	BWENC_VID_RC_VBR,

	BWENC_VID_RC_NB
} eVID_RC;

typedef enum
{
	BWENC_AUD_CODEC_INVALID = 0,

	BWENC_AUD_CODEC_MP2,
	BWENC_AUD_CODEC_AAC_LC,
	BWENC_AUD_CODEC_AAC_HE,

	BWENC_AUD_CODEC_NB
} eAUD_CODEC;

typedef enum
{
	BWENC_OSD_TYPE_INVALID = 0,

	BWENC_OSD_TYPE_TEXT,
	BWENC_OSD_TYPE_LOGO,
	BWENC_OSD_TYPE_QRCODE,

	BWENC_OSD_TYPE_NB
} eOSD_TYPE;

typedef enum
{
	BWENC_AUD_SMPFMT_INVALID = 0,

	BWENC_AUD_SMPFMT_S16,
	BWENC_AUD_SMPFMT_S8,

	BWENC_AUD_SMPFMT_NB
} eSMP_FMT;

typedef enum
{
	BWENC_VID_DAR_INVALID = 0,
	BWENC_VID_DAR_4_3,
	BWENC_VID_DAR_16_9,
	BWENC_VID_DAR_NB
} eVID_DAR;


// ethio
typedef struct ENC_ETHIOSET_
{
	U8 ethio_isenable;
	U8 ethio_ethid;                   
	U16 ethio_sipport;
	U32 ethio_sipaddr;
	U16 ethio_dipport;
	U8	reserve[2];
	U32 ethio_dipaddr;
	U32 ethio_bitrates;
} stENC_ETHIOSET, *pENC_ETHIOSET;

//video set
typedef struct _ENC_VIDSET
{
	U8 vid_codec;			// codec type	h.264 or h.265
	U8 vid_profile;			// profile
	U8 vid_level;			// reserved
	U8 vid_rctype;			// rate control
	U16 vid_width;
	U16 vid_height;
	U32 vid_bitrate_kb;		// bitreate, kbs
	U32 vid_bitrate_min;
	U32 vid_bitrate_max;
	U16 vid_gopsize;
	U16 vid_fpsnum;
	U16 vid_fpsden;
	U16	vid_dar;			// display aspect ratio
	//U8  vid_reserve[16];	// reserved
	U8  vid_fluctuate;
	U8  vid_reserve[15];// reserved
} stENC_VIDSET, *pENC_VIDSET;

typedef struct _ENC_LOGO
{
	U8 logo_url[ENC_OSD_MAX_SLIDE_NUM][MAX_PATH];
	U8	logo_opacity;
	U8	logo_num;
	U16 logo_width;
	U16 logo_height;
	U16 logo_x;
	U16 logo_y;
	U8	logo_movdir;
	U8  logo_movspeed;
} stENC_LOGOSET, *pENC_LOGOSET;

typedef struct _ENC_QRCODE
{
	U8  qr_url[MAX_PATH];//二维码图片logo
	U32	qr_opacity;
	U16 qr_width;
	U16 qr_height;
	U16 qr_x;
	U16 qr_y;
} stENC_QRCODE, *pENC_QRCODE;

typedef struct _ENC_SUB
{
	U8  sub_content[ENC_OSD_SUB_LEN];
	U8  sub_font[ENC_OSD_FONTNAME_LEN];
	U16 sub_fontsize;
	U16	sub_fontstyle;
	U32	sub_fontcolor;
	U32 sub_backcolor;
	U16 sub_x;			//字幕条坐标
	U16 sub_y;
	U16	sub_width;		//字幕条大小
	U16	sub_height;
	U16	sub_movdir;
	U16	sub_movspeed;
} stENC_SUBSET, *pENC_SUBSET;

typedef struct _ENC_OSD_TM
{
	U32	tm_start_sec;	//seconds,  00:00 -- now
	U32	tm_end_sec;
} stENC_OSD_TMSET, *pENC_OSD_TMSET;

// osd set
typedef struct _ENC_OSDSET
{
	U8 			osd_enable;
	U8		 	osd_type;
	U8          priority;	//优先级
	U8			reserve;	//padding
	union
	{
		stENC_LOGOSET 	osd_logo;
		stENC_SUBSET 	osd_sub;
		stENC_QRCODE	osd_qr;
	} osd_cfg;

	stENC_OSD_TMSET	osd_effect_tm[ENC_WEEK_DAYS][ENC_OSD_MAX_TM_NUM];
} stENC_OSDSET, *pENC_OSDSET;

// audio set
typedef struct _ENC_AUDSET
{
	U8 aud_codec;		// mp2, lc-aac, he-aac
	U8 aud_vol;			// volume
	U16 aud_bitrate_kb;
	U16 aud_smp_rate;	// 48000, 44100 hz
	U8  aud_smp_fmt;	// 16, 8bit
	U8  aud_channle;	// stereo or mono
} stENC_AUDSET, *pENC_AUDSET;

// ts set
typedef struct _ENC_TSSET
{
	U8		ts_service_name[ENC_SERVICE_NAME_LEN];
	U8		ts_provider_name[ENC_PROVIDER_NAME_LEN];
	U8		reserver[2];	//padding
	U16		ts_pmt_pid;
	U32		ts_service_id;
	U16		ts_pcr_pid;
	U16		ts_vid_pid;
	U16		ts_aud_pid;
	U16		ts_pcr_interval_ms;
	U32		ts_muxrate_kb;
}stENC_TSSET, *pENC_TSSET;

// encoder channel set
typedef struct _ENC_CHLSET {
	U8  			encchl_setvsion;               //update by webapp
	U8  			encchl_savevsion;              //update by monitor
	U8  			encchl_curvsion;               //update by encoder

	U8  			encchl_actcount;               //state, update by app
	U32 			encchl_procpid;

	U8  			encchl_isrun;
	U8				encchl_reset_chl;				// need restart encoder channel
	U8				encchl_ismptssel;				// need mux to mpts
	U8				reserve;						// 

	stENC_VIDSET	encchl_vid;
	stENC_OSDSET	encchl_osd[ENC_OSD_MAX_OVERLAY];
	stENC_AUDSET	encchl_aud;
	stENC_TSSET		encchl_ts;
	stENC_ETHIOSET	encchl_output;					// spts output to FPGA, can not set by user

	U32				encchl_errcode;
}stENC_CHLSET, *pENC_CHLSET;

typedef struct _ENC_IPIN
{
	U8				inp_enable;
	U8				inp_igmp;			// v1, v2, v3
	U16				inp_proto;			// reserved, only support UDP this version
	stENC_ETHIOSET	inp_ethio;
} stENC_IPINSET, *pENC_IPINSET;

typedef struct _ENC_MUX
{
	U32			mux_maxrate_kb;
	U16			mux_origin_nid;			//Original Network ID
	U16			mux_netid;				//Network ID
	U16			mux_transid;			//Transport ID
	U16			mux_pcr_correct;		// 0/1
} stENC_MUXSET, *pENC_MUXSET;

typedef struct _ENC_OUTPUT
{
	U8				out_proto;			// reserved, only support UDP this version
	U8				out_filte_nullpkt;	// 0/1
	U8				out_enable_ip;		// enable ip output
	U8				out_enable_asi;		// enable asi output 
	U32				out_status;
	U32				out_bitrate_kb;
	stENC_ETHIOSET	out_ethio;
} stENC_OUTSET, *pENC_OUTSET;

typedef struct _SUB_BOARD_INFO
{
	U16				status;
	U16				reg_flag;
}stSUB_BOARD_INFO, *pSUB_BOARD_INFO;

// master
typedef struct _ENC_DEVSET_M {
	U8  			enc_inited;                  //constant, init by minitor
	U8  			enc_appvsion;                //constant, init by minitor

	U8  			enc_setvsion;                //update by webapp
	U8  			enc_savevsion;               //update by minitor
	U16  			enc_curvsion;                //update by app

	U16  			enc_ethdevnum;               //constant, init by minitor
	U8  			enc_ethdevname[ENC_ETHIFMAX_MASTER][ENC_ETHNAME_LEN];     //constant, init by minitor
	U32   			enc_ethdevipaddr[ENC_ETHIFMAX_MASTER][ENC_ETHIPMAX];   //setdef by minitor, update by webapp

	stENC_CHLSET	enc_chls[ENC_TOTAL_CHLMAX];

	// ip input
	// MAX 32 MPTS/SPTS input
	stENC_IPINSET	enc_ipin[ENC_MAX_IPINPUT_NUM];
	
	// output for enc channel
	stENC_OUTSET	enc_output[ENC_TOTAL_CHLMAX];		// 24 SPTS

	// mpts mux
	stENC_MUXSET	enc_mptsmux;
	stENC_OUTSET	enc_mptsout;

	// sub board info
	stSUB_BOARD_INFO	sboard[ENC_MAX_SUB_BOARD];
}stENC_DEVSET_MASTER, *pENC_DEVSET_MASTER;

typedef struct _ENC_CHAN_SHARE
{
	U32 sys_init;
	U32 i2c0_init;
	U32 i2c1_init;
	sem_t i2c0_lock;
	sem_t i2c1_lock;
	U32 ai_demux_run;
}stENC_CHLSHARE;

// slave
typedef struct _ENC_DEVSET_S {
	U8  			enc_inited;                  //constant, init by minitor
	U8  			enc_appvsion;                //constant, init by minitor

	U8  			enc_setvsion;                //update by webapp
	U8  			enc_savevsion;               //update by minitor
	U16  			enc_curvsion;                //update by app

	U16  			enc_ethdevnum;               //constant, init by minitor
	U8  			enc_ethdevname[ENC_ETHIFMAX_SLAVE][ENC_ETHNAME_LEN];     //constant, init by minitor
	U32   			enc_ethdevipaddr[ENC_ETHIFMAX_SLAVE][ENC_ETHIPMAX];   //setdef by monitor

	stENC_CHLSHARE  enc_chlshare;
	stENC_CHLSET	enc_chls[ENC_MAX_CHL];

}stENC_DEVSET_SLAVE, *pENC_DEVSET_SLAVE;


#endif /* ENC_SHRM_H_ */
